package com.zj.cman.modules.articles.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zj.cman.modules.articles.entity.Article;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface ArticleMapper extends BaseMapper<Article> {

    @Select("<script>" +
            "SELECT * FROM articles a " +
            "<where>" +
            "<if test='categoryIds != null and categoryIds.size() > 0'>" +
            "EXISTS (SELECT 1 FROM article_categories ac WHERE ac.article_id = a.id AND ac.category_id IN" +
            "<foreach collection='categoryIds' item='id' open='(' separator=',' close=')'>#{id}</foreach>)" +
            "</if>" +
            "<if test='tagIds != null and tagIds.size() > 0'>" +
            "AND EXISTS (SELECT 1 FROM article_tags at WHERE at.article_id = a.id AND at.tag_id IN" +
            "<foreach collection='tagIds' item='id' open='(' separator=',' close=')'>#{id}</foreach>)" +
            "</if>" +
            "</where>" +
            "</script>")
    IPage<Article> selectArticlesByCategoriesAndTags(Page<Article> page, @Param("categoryIds") List<Long> categoryIds, @Param("tagIds") List<Long> tagIds);

}
